package com.teradata.iodata.config.jpaconfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableJpaRepositories(basePackages = "com.teradata.iodata.repositories")
public class JpaConfig {

    private static final String PUNIT_NAME = "Tutorial";

    @Autowired
    private DataSource dataSource;

    @Bean
    public EntityManagerFactory entityManagerFactory() {

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        //EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PUNIT_NAME);
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setPersistenceUnitName(PUNIT_NAME);
        factory.setJpaVendorAdapter(vendorAdapter);
        //此处com.example.*.model是你的java bean所在的包名
        factory.setPackagesToScan("com.iLearn.iodata.entities");
        factory.setDataSource(dataSource);

        Map<String, Object> jpaProperties = new HashMap<String, Object>();
        jpaProperties.put("hibernate.ejb.naming_strategy","org.hibernate.cfg.ImprovedNamingStrategy");
        jpaProperties.put("hibernate.jdbc.batch_size",50);
        jpaProperties.put("hibernate.show_sql","true");
        jpaProperties.put("hibernate.format_sql","true");


        /*jpaProperties.put("eclipselink.logging.level.sql","FINEST");
        jpaProperties.put("eclipselink.logging.parameters","true");
        jpaProperties.put("eclipselink.logging.timestamp","true");
        jpaProperties.put("eclipselink.weaving","static");
        jpaProperties.put("eclipselink.persistence-context.flush-mode","commit");
        jpaProperties.put("javax.persistence.validation.mode","NONE");
        jpaProperties.put("javax.persistence.jdbc.url","jdbc:hsqldb:mem:com.sample");
        jpaProperties.put("javax.persistence.jdbc.driver","org.hsqldb.jdbcDriver");*/

        factory.setJpaPropertyMap(jpaProperties);
        factory.afterPropertiesSet();
        return factory.getObject();
    }




}
